<template>
    <div>
        <div class="bg-banner" />
        <div v-if="next" id="forgot-box">
            <el-form ref="form" :model="form" :rules="rules" class="forgot-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">通过手机/邮箱找回密码</h3>
                </div>
                <div>
                    <el-form-item prop="phone">
                        <el-input ref="phone" v-model="form.phone" placeholder="登录手机号/邮箱" type="text" tabindex="1" auto-complete="on" />
                    </el-form-item>
                    <el-form-item prop="verify">
                        <el-input ref="password" v-model="form.verify" placeholder="验证码" tabindex="2" auto-complete="on">
                            <svg-icon slot="suffix" name="update" />
                        </el-input>
                    </el-form-item>
                </div>
                <el-button type="primary" style="width: 100%;" @click="toNext('toSubmit')">下一步</el-button>
            </el-form>
        </div>
        <Copyright v-if="$store.state.settings.showCopyright" />
        <div v-if="submit" id="forgot-next-box">
            <el-form ref="abcdefg" class="forgot-next-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">找回密码</h3>
                </div>
                <div>
                    <el-form-item prop="test">
                        <el-input ref="test" placeholder="请输入短信验证码" type="text" tabindex="1" auto-complete="on" />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" :type="passwordType" placeholder="8-20位,字母/数字/字符至少包含两种" tabindex="2" auto-complete="on">
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                </div>
                <el-button type="primary" style="width: 100%;" @click="toNext('toFinish')">提交</el-button>
            </el-form>
        </div>
        <div v-if="finish" id="finish-box">
            <el-form ref="abbbb" class="finish-form" label-position="left">
                <svg-icon name="finish" />
                <div class="title-container">
                    <h3 class="title">密码设置成功</h3>
                </div>
                <div>
                    <span>请谨慎保管和记忆你的密码，以免泄露和丢失</span>
                </div>
                <el-button type="primary" style="width: 50%;">完成{{ time }}</el-button>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name: 'Forgot',
    data() {
        return {
            next: true,
            submit: false,
            finish: false,
            time: 0,
            passwordType: 'password',
            form: {
                phone: localStorage.login_account || '', // 创建一个本地存储的name/value对,保存网站数据
                verify: '' // 先置空
            },
            rules: { // 校验
                phone: [
                    { required: true, trigger: 'blur', message: '请输入手机号或邮箱' } // 没输入时会弹出提醒
                ],
                verify: [
                    { required: true, trigger: 'blur', message: '请输入验证码' }
                ]
            }
        }
    },
    watch: {
        'time': function(newVal, oldVal) {
            if (newVal === 5) {
                setInterval(this.countDown, 1000)
            } else if (newVal === 0) {
                this.$router.push('/login')// 你想要跳转的页面路径
            }
        }
    },
    methods: {
        countDown() {
            this.time--
        },
        toNext(type) {
            if (type === 'toSubmit') {  // 页面跳转
                this.$refs.form.validate().then(rsp => {
                    if (rsp) {
                        this.next = false
                        this.submit = true
                    }
                })
            } else if (type === 'toFinish') { // 即我点击提交按钮的时候开始倒计时
                this.next = false
                this.submit = false
                this.finish = true
                this.time = 5
            }
        },
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
        }
    }
}
</script>

<style lang="scss" scoped>
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/login-bg2.jpg);
    opacity: 0.2;
    background-size: cover;
    background-position: center center;
}
.title-container {
    position: relative;
    .title {
        font-size: 22px;
        color: #1a1a1a;
        margin: 0 auto 30px;
        text-align: center;
        font-weight: bold;
        @include text-overflow;
    }
}
#forgot-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .forgot-form {
        width: 400px;
        padding: 50px 35px 30px;
        overflow: hidden;
        background-color: #faf4f4;
        .svg-icon {
            vertical-align: -0.35em;
        }
    }
}
#forgot-next-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .forgot-next-form {
        width: 400px;
        padding: 50px 35px 30px;
        overflow: hidden;
        background-color: #faf4f4;
        .svg-icon {
            vertical-align: -0.35em;
        }
    }
}
#finish-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .finish-form {
        width: 400px;
        padding: 50px 35px 30px;
        overflow: hidden;
        background-color: #faf4f4;
        .svg-icon {
            width: 20em;
            height: 5em;
        }
        .title {
            margin-top: 0.5em;
        }
        .el-button {
            margin-left: 6em;
            margin-top: 2em;
        }
    }
}
.copyright {
    position: absolute;
    bottom: 80px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
    font-size: 12px;
}
</style>
